<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/15
 * Time: 9:34
 */
namespace app\home\model;

use app\home\model\Base;
use think\Model;
use think\Request;
use think\Db;
use think\Exception;
use think\Log;
use think\Session;
use wxpay\Wxpay;

class Renew extends Base {

    /**
     * 构造
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 卡片验证规则：
     * 1、属于当前登陆用户
     * 2、已激活、有效期内已停卡的卡片
     */
    public function checkCardM()
    {
        try {
            $param = Request::instance()->param();
            if(!$param['iccid']){
                return ['code'=>1,'msg'=>'请输入续费卡片ICCID'];
            }
            $sql = "SELECT a.`id`,a.`iccid`,a.`pid`,c.`globalsimtype` FROM yc_agent_card AS a LEFT JOIN yc_card AS c ON a.`iccid` = c.`iccid` WHERE a.`iccid` LIKE '".$param['iccid']."%' AND a.`cid` = ".$this->user_info['id'];
            $sql .= " AND a.`aid` = ".$this->user_info['agent_id']." AND c.`dateexpire` > ".time()." AND c.`status` in (3,4)";
            $result = Db::query($sql);
            if(!$result || count($result) > 1){   // 没数据或者查到多个数据
                return ['code'=>1,'msg'=>'输入的ICCID有误，请核对'];
            }
            return ['code'=>0,'msg'=>'ICCID符合续费条件','card'=>$result];
        } catch (Exception $e) {
            Log::write('验证续费卡片异常：'.$e->getMessage(),'error');
        }
    }

    /**
     * 卡片续费
     */
    public function confirmRenewM()
    {
        try {
            $param = Request::instance()->post();
            // 验证续费周期
            if(!in_array($param['cycle'],$this->renew_type)){
                return ['code'=>1,'msg'=>'请选择正确的续费周期'];
            }
            // 验证卡片
            $check = $this->checkCardM();
            if($check['code']){
                return ['code'=>2,'msg'=>'续费失败'];
            }
            // 获取续费的套餐信息
            $package = $this->getCardPackageM($check['card'][0]['pid']);
            if($package['code']){
                return ['code'=>3,'msg'=>'续费失败'];
            }
            // 获取续费金额
            $renew_price = $package['package']['pkg_price']*$param['cycle']*100;
            // 验证账户余额是否满足
            $balance = model('Personal')->getMyAccountBalanceM();
            if( ($balance*100) < $renew_price ){
                return ['code'=>4,'msg'=>'账户余额不足，请先充值'];
            }
            $order_info = [
                'iccid'=>[$check['card'][0]["id"]],  // agent_card中的记录id
                'cycle'=>$param['cycle'],  // 续费周期
                'operator'=>$check['card'][0]['globalsimtype'],  // 运营商
            ];
            // 验证成功后，生成续费订单
            $create_order = $this->createOrder($renew_price, '04', 4, json_encode($order_info,JSON_UNESCAPED_UNICODE), 1, '', []);
            if(!$create_order){  // 订单创建失败,并生成续费记录
                $renew_log = $this->createLog($param['iccid'],'卡片续费',false);
                return ['code'=>5,'msg'=>'续费失败'];
            }
            // 生成续费记录
            $renew_log = $this->createLog($param['iccid'],'卡片续费',true);
            return ['code'=>0,'msg'=>'续费成功，请耐心等待审核'];
        } catch (Exception $e) {
            Log::write('确认续费异常：'.$e->getMessage(),'error');
        }
    }

}